package controllers

import (
	"github.com/gin-gonic/gin"
	"mishop/models"
	"mishop/services"
	"strconv"
)

type ITrade interface {
	FindByID(ctx *gin.Context)
}
type Trade struct {
	service services.ITradeService
}

func (c *Trade) FindByID(ctx *gin.Context) {
	product_id := ctx.Param("product_id")
	productID, err := strconv.Atoi(product_id)
	if err != nil {
		ctx.JSON(400, gin.H{
			"msg": "OK",
		})
		return
	}
	product, err := c.service.FindByID(uint(productID))
	if err != nil {

		ctx.JSON(400, gin.H{
			"msg": "OK" + err.Error(),
		})
		return
	}

	ctx.JSON(200, gin.H{
		"msg":  "OK",
		"data": product,
	})
}

func NewTrade() ITrade {
	service := services.NewTradeService(models.NewTrade(models.Db))
	return &Trade{service: service}
}
